<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id = "start">start</button>
    <button id = "stop">stop</button>
    <div id ="result">
    </div>
    <script src="webworker.js"></script>
    <script>
        let startBtn = document.getElementById('start');
        let stopBtn = document.getElementById('stop');
        startBtn.addEventListener('click', startWorker,false);
        stopBtn.addEventListener('click',stopWork,false);
        var w;
        function startWorker() {
            if(typeof(Worker) !=="undefined") { // 浏览器的嗅探 是否支持Work 功能
                if(typeof(w) == "undefined") { // 只能实例化一次
                    w = new Worker("webworker.js")
                }
                w.onmessage = function(event) {
                    result.innerHTML = event.data
                }
            }
            else {
                result.innerHTML = "sory ,you browser 不支持"
            }
        }
        function stopWork() {
            if(w) {
                w.terminate();
            }
        }
    </script>
</body>
</html>